<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['type']){
	$_SESSION['choose_source']['type']=trim($_RQ['type']);
}
if($_RQ['source_type']){
	$_SESSION['choose_source']['source_type']=trim($_RQ['source_type']);
}
$return['type']=$_SESSION['choose_source']['type'];
$return['source_type']=$_SESSION['choose_source']['source_type'];
if($return['source_type']=='image'){
	$return['typename']='图片';
}elseif($return['source_type']=='video'){
	$return['typename']='视频';
}elseif($return['source_type']=='audio'){
	$return['typename']='音频';
}else{
	$return['typename']='素材';
}
$tabs=array(
	array('op'=>'local','title'=>'本地上传'),
	array('op'=>'remote','title'=>'提取网络素材'),
	array('op'=>'srcurl','title'=>'远程网络地址'),
	array('op'=>'list','title'=>$return['typename'].'库选取'),
);
$return['tabs']=$tabs;

if($_RQ['op']=='local' || $_RQ['op']=='remote' || $_RQ['op']=='srcurl'){
	$return['item']=array();
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>$return['source_type']);
	if(isset($_RQ['group_id'])){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}else{
		//默认显示系统分组
		$params['group_id']=-1;
		$return['params']['group_id']=-1;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('title','attachment');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$list=pdo_getslice('core_attachment',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['groups']=pdo_getall('core_attachment_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$nogroup=array('id'=>0,'title'=>'未分组');
	array_unshift($return['groups'],$nogroup);
	$all=array('id'=>-1,'title'=>'系统');
	array_unshift($return['groups'],$all);
	exi($return);
}
if($_RQ['op']=='wechat_image_url'){
	//将选中的图片地址转换为微信图片地址
	$list=str_array($_RQ['imagelist']);
	foreach($list as &$image){
		$image=cfc('files')->wechat_image_url($image);
	}
	exi($list);
}